package com.tiger.ebenv.sample3;

import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.util.*;

public class StockPriceServer extends HttpServlet {
    public final static String companyNameList[] = {
        "ABC Company",
        "Zebra Corp.",
        "Rock Site Company"
    };
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        response.setContentType("application/xml; charset=\"utf-8\"");
        java.io.PrintWriter out = response.getWriter();
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        DateFormat formatter = DateFormat.getDateTimeInstance(
        DateFormat.LONG, DateFormat.LONG);
        out.println("<stockPrices>");
        out.print("<date>");
        out.print(formatter.format(new Date()));
        out.println("</date>");
        
        for(int i = 0; i < companyNameList.length; i++) {
            out.println("<stockPrice>");
            out.print("<company>");
            out.print(companyNameList[i]);
            out.println("</company>");
            out.print("<price>");
            out.print(Math.random() * 1000.0);
            out.println("</price>");
            out.println("</stockPrice>");
        }
        out.println("</stockPrices>");
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
}
